﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoftwareConsulting.BI11.Studio.Core.Project.Metadata;
using SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.Extensions;
using SoftwareConsulting.Interfaces.BasicEntities;
using SoftwareConsulting.BI11.Studio.InternalServices;
using SoftwareConsulting.BI11.Controls.Metadata;

namespace SoftwareConsulting.BI11.Studio.UI.ProjectExplorer.Dimensions
{
    public partial class DimensionViewerForm : RestorableForm
    {
        private IDimension _dimension; //просматриваемое измерение

        /// <summary>
        /// Сворачивает всю иерархию
        /// </summary>
        private void CollapseAll()
        {
            ItemsTree.CollapseAll();
        }

        /// <summary>
        /// Разворачивает всю иерархию
        /// </summary>
        private void ExpandAll()
        {
            ItemsTree.ExpandAll();
        }

        /// <summary>
        /// Рекурсивно строит иерархию
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="items"></param>
        private void BuildHierarchyRecursively(TreeNodeCollection nodes, INamedHierarchy<INamed> items)
        {
            int c = items.ItemsCount;
            for (int i = 0; i < c; i++)            
            {
                INamed curItem = items.GetItemByPosition(i, false);
                TreeNode node = nodes.Add(curItem.Name);
                var h = curItem as INamedHierarchy<INamed>;
                if (h != null)
                {
                    BuildHierarchyRecursively(node.Nodes, h);
                }
            }
        }

        /// <summary>
        /// Инициализирует диалог
        /// </summary>
        private void Initialize()
        {
            this.SetObjectViewerCaption(this.Text, _dimension);
            this.Icon = MetadataObjectKind.Dimension.GetIcon();
            //строим дерево
            TreeNodeCollection nodes = ItemsTree.Nodes;
            _dimension.Update();
            INamedHierarchy<INamed> items = _dimension.Items;
            BuildHierarchyRecursively(nodes, items);            
            //сворачиваем дерево
            CollapseAll();
        }

        public DimensionViewerForm(IDimension dimension)
        {
            _dimension = dimension;
            InitializeComponent();
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void DimensionViewerForm_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        private void развернутьВсюИерархиюToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExpandAll();
        }

        private void свернутьВсюИерархиюToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CollapseAll();
        }
    }
}
